import {Component,OnInit,ViewChild} from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { PageModel } from "../../../../model/page-model";
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
@Component({
  selector: 'app-grouplist',
  templateUrl: './grouplist.component.html',
  styleUrls: ['./grouplist.component.css']
})

export class GrouplistComponent implements OnInit { 
  @ViewChild('myModal') public myModal:ModalDirective;
  pageModel = new PageModel();
  page:number = 1;
  name:any;
  IDnum:any;
  phone:any;
  btype:number;
  group_name_new:any;
  group_address_new:any;
  group_name_edit:any;
  group_address_edit:any;
  groupdata:any;
  group_id:any;
  deviceIds:any;
  groupstat:any;

  constructor(private myHttp:MyHttp) {}
  ngOnInit() {   
  	this.getdata();
  }

  getdata(){    
    this.myHttp.post('group/findAll',{}).subscribe(res=>{
      if(res.errorCode==0){
         this.groupdata = res.message;
        
         console.log(this.groupdata)
      }    
      
    })
  }

  savedata(){
    
    this.myHttp.post('group/create',
    {
      "group_name"  : this.group_name_new,
      "group_address" : this.group_address_new,     
    }).subscribe(res=>{
      if(res.errorCode==0){
        alert(res.message);
        this.myModal.hide();
        this.group_name_new='';
        this.group_address_new='';
        this.getdata();
      }
    });
  }

  edit(obj){
  	this.myModal.show();
  	this.btype = 2;
  	this.group_name_edit = obj.group_name;
  	this.group_address_edit = obj.group_address;
  	this.group_id = obj.id;

  }
  saveedit(){
  	this.myHttp.post('group/update',
    {
      "group_id":this.group_id,
      "group_name"  : this.group_name_edit,
      "group_address" : this.group_address_edit,     
    }).subscribe(res=>{
      if(res.errorCode==0){
      	//console.log(res)
        alert(res.message);
        this.myModal.hide();       	
        this.getdata();
      }
    });
  }

  showdevices(obj){
  	this.myModal.show();
  	this.group_id = obj.id;  	
  	this.btype = 5;
  }

  savegroupdevices(){
  	this.myHttp.post('group/updateDevices',
    {
      "group_id":this.group_id,
      "group_devices" :this.deviceIds 
    }).subscribe(res=>{
    	//console.log(res)

      if(res.errorCode==0){
        //console.log(res)
        alert(res.message);
        this.myModal.hide();       	
        this.getdata();
      }else{
      	alert(res.message);
      	this.myModal.hide();       	
        this.getdata();
      }
    });
  }

  showstatus(obj){
  	this.myModal.show();
  	this.btype = 3;
  	this.group_id = obj.id;
  	this.groupstat = obj.group_status;
  }
  groupstatus(){
  	this.myHttp.post('group/updateStatus',
    {
      "group_id":this.group_id,
      "group_status" :this.groupstat=(this.groupstat==0)?1:0
    }).subscribe(res=>{
      if(res.errorCode==0){
        //console.log(res)
        alert(res.message);
        this.myModal.hide();       	
        this.getdata();
      }
    });
  }


  showpolicy(obj){
  	this.group_id = obj.id;
  	this.myModal.show();
  	this.btype = 4;
  }

  savepolicy(){
  	this.myHttp.post('group/updatePolicy',
    {
      "group_id":this.group_id,
      "policy_id":0   
    }).subscribe(res=>{
      if(res.errorCode==0){
        //console.log(res)
        alert(res.message);
        this.myModal.hide();       	
        this.getdata();
      }
    });
  }

  cancel(){
    this.name="";
    this.IDnum="";
    this.phone="";
  }

    




}
